package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.RolePermissionDto;
import com.jyy.prefabricated.entity.RolePermission;
import com.jyy.prefabricated.entity.SysRole;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.RolePermissionMapper;
import com.jyy.prefabricated.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0.0
 * @author: zjj
 */
@Transactional
@Service
public class SysRoleService {

    @Autowired
    private SysRoleMapper roleMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 新增
     * @param role
     * @return
     */
    public int insert(SysRole role){
        if(checkExist(role)!= null){
            throw new CustomException("当前角色名已存在！");
        }
        role.setStatus(BaseStatusEnum.VALID.getValue());
        roleMapper.insert(role);
        return role.getId();
    }

    /**
     * 更新
     * @param role
     * @return
     */
    public int update(SysRole role){
        SysRole checkRole = checkExist(role);
        if(checkRole!= null && !checkRole.getId().equals(role.getId())){
            throw new CustomException("当前角色名已存在！");
        }
        if(role.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return roleMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 重复校验
     * @param sysRole
     * @return
     */
    public SysRole checkExist(SysRole sysRole){
        SysRole role = roleMapper.selectByName(sysRole.getRoleName());
        return role;
    }

    /**
     * 更新状态
     * @param id
     * @return
     */
    public int updateStatus(Integer id){
        SysRole sysRole = roleMapper.selectByPrimaryKey(id);
        if(sysRole.getStatus().equals(BaseStatusEnum.VALID.getValue())){
            sysRole.setStatus(BaseStatusEnum.INVALID.getValue());
        } else {
            sysRole.setStatus(BaseStatusEnum.VALID.getValue());
        }
        return roleMapper.updateByPrimaryKeySelective(sysRole);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return roleMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public SysRole selectById(Integer id){
        return roleMapper.selectByPrimaryKey(id);
    }

    /**
     * 根据名称查询
     * @param roleName
     * @return
     */
    public List<SysRole> selectByRoleName(String roleName){
        return roleMapper.selectByRoleName(roleName);
    }

    /**
     * 查询所有角色列表
     * @return
     */
    public List<SysRole> selectAll(){
        return roleMapper.selectAll();
    }

    /**
     * 查询角色列表
     * @param status 状态
     * @return
     */
    public List<SysRole> selectByParams(Integer status){
        return roleMapper.selectByParams(status);
    }

    /**
     * 设置角色权限
     * @param dto
     */
    public void putPermission(RolePermissionDto dto){
        if(dto.getPermissionIds().size() == 0){
            //如果选中的权限为空，则其清空权限列表
            rolePermissionMapper.deleteByRoleId(dto.getRoleId());
        } else {
            //先清空权限列表
            rolePermissionMapper.deleteByRoleId(dto.getRoleId());
            List<RolePermission> rolePermissions = new ArrayList<>();
            for(Integer permissionId : dto.getPermissionIds()){
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(dto.getRoleId());
                rolePermission.setPermissionId(permissionId);
                rolePermissions.add(rolePermission);
            }
            rolePermissionMapper.insert(rolePermissions);
//            if(row == 1){
//                throw new CustomException("配置权限失败！");
//            }
        }
    }
}
